//
//  MegaphoneViewController.m
//  AndCampus
//
//  Created by 史宵宵 on 2017/12/19.
//  Copyright © 2017年 史宵宵. All rights reserved.
//

#import "MegaphoneViewController.h"
#import "MegaphoneDetailViewController.h"

@interface MegaphoneViewController ()
@property (nonatomic,strong) NSMutableArray *dataArray;
@end

@implementation MegaphoneViewController
- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"传声筒";
    [self creatData];
    UIImage *image = [UIImage imageNamed:@"megaphone_bg"];
    self.view.layer.contents = (id) image.CGImage;
}
- (void)creatData{
    YJNetWorkManger *yjNetWorkManger =  [YJNetWorkManger shareManager];
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    if ([[JHUserInfo getUserInfo].role_type isEqualToString:@"1"]) {
        [dic setValue:self.childId forKey:@"parent_id"]; //家长的id
    }else{
        [dic setValue:self.childId forKey:@"child_id"]; //孩子的id
    }
    [dic setValue:[Global getUserTokenInfo] forKey:@"token"]; //用户的token
    
    [yjNetWorkManger requestType:POST andURL:[JSPathUtil urlAppSoundGetSoundLibList] andParmas:dic andComplition:^(id response, BOOL isuccess) {
        if (isuccess) {
            [self.dataArray removeAllObjects];
            [self.dataArray addObjectsFromArray:[NSMutableArray arrayWithArray:response]];
            [self creatMenu];
        }
    }];
}
- (void)creatMenu{
    
    NSInteger num = 4;
    double width = 60;
    double margin = (kScreenW - num*60)/(num + 1);
    for (int i = 0; i <self.dataArray.count; i ++) {
        UIButton *menuButton = [[UIButton alloc]init];
        menuButton.frame = CGRectMake(margin + (margin + width) * (i % num), 100 + (margin + width) * (i / num), width, width);
        menuButton.tag = 1000 + i;
        if ([self.dataArray[i][@"type"] intValue] == 1) {  //1 ：双方答完  2 家长答完 3孩子答完 4双方未答
            [menuButton setBackgroundImage:[UIImage imageNamed:@"Megaphone_all_icon"] forState:UIControlStateNormal];
        }else if ([self.dataArray[i][@"type"] intValue] == 2){
            [menuButton setBackgroundImage:[UIImage imageNamed:@"Megaphone_person_icon"] forState:UIControlStateNormal];
        }else if ([self.dataArray[i][@"type"] intValue] == 3){
            [menuButton setBackgroundImage:[UIImage imageNamed:@"Megaphone_child_icon"] forState:UIControlStateNormal];
        }else{
            [menuButton setBackgroundImage:[UIImage imageNamed:@"Megaphone_none_icon"] forState:UIControlStateNormal];
        }
        menuButton.tag = 1000 + i;
        [menuButton addTarget:self action:@selector(addPassLog:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:menuButton];
    }
    
}
- (void)addPassLog:(UIButton *)sender{
    NSDictionary *dic = self.dataArray[sender.tag - 1000];
    if ([dic[@"can_click"] intValue] ==0) { //0不可以点击进去 1可以点击进去
         [Global showActivityWithView:self.view withText:@"前面的问题还没有答完"];
    }else{
        MegaphoneDetailViewController *megaphoneDetailVC = [[MegaphoneDetailViewController alloc]init];
        megaphoneDetailVC.child_id = self.childId;
        megaphoneDetailVC.sound_id = dic[@"id"];
        __weak typeof(self) unself = self;
        megaphoneDetailVC.throwSuccessBlack=^(){
            [unself creatData];
        };
        [self.navigationController pushViewController:megaphoneDetailVC animated:YES];
    }
//    if ([dic[@"type"] intValue] == 1) {  //1 ：双方答完 展示答案页面
//        MegaphoneDetailViewController *megaphoneDetailVC = [[MegaphoneDetailViewController alloc]init];
//        [self.navigationController pushViewController:megaphoneDetailVC animated:YES];
//    }else if (sender.tag - 1000 == 0 ||[dic[@"type"] intValue] == 2 || [dic[@"type"] intValue] == 2) {  //2 家长答完 3孩子答完 进答问题页面
//        MegaphoneDetailViewController *megaphoneDetailVC = [[MegaphoneDetailViewController alloc]init];
//        [self.navigationController pushViewController:megaphoneDetailVC animated:YES];
//    }else{ //
//        [Global showActivityWithView:self.view withText:@"前面的问题还没有答完"];
//        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message: [NSString stringWithFormat:@"解锁此关卡需要消耗%@积分",dic[@"value"]] preferredStyle:UIAlertControllerStyleAlert];
//
//        // Create the actions.
//        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
//
//        }];
//
//        UIAlertAction *otherAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
//            YJNetWorkManger *yjNetWorkManger =  [YJNetWorkManger shareManager];
//            NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
//            [dic setValue:[Global getUserTokenInfo] forKey:@"token"]; //用户的token
//            [dic setValue:self.dataArray[sender.tag - 1000][@"id"] forKey:@"pass_id"]; //关卡id
//            [yjNetWorkManger requestType:POST andURL:[JSPathUtil urlAppInterestAddPassLog] andParmas:dic andComplition:^(id response, BOOL isuccess) {
//                if (isuccess) {
//                    [sender setBackgroundImage:[UIImage imageNamed:@"ins_watch_menu_select"] forState:UIControlStateNormal];
//                    [sender setTitle:[NSString stringWithFormat:@"%ld",sender.tag - 1000 + 1] forState:UIControlStateNormal];
//                    //解锁成功进详情页
//                    MegaphoneDetailViewController *megaphoneDetailVC = [[MegaphoneDetailViewController alloc]init];
//                    [self.navigationController pushViewController:megaphoneDetailVC animated:YES];
//                }
//            }];
//        }];
//
//        // Add the actions.
//        [alertController addAction:cancelAction];
//        [alertController addAction:otherAction];
//        [self presentViewController:alertController animated:YES completion:nil];
//    }
}
- (void)enterWatch:(UIButton *)sender{
//    WatchProblemViewController *watchProblemVC = [[WatchProblemViewController alloc]init];
//    [self.navigationController pushViewController:watchProblemVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
